package com.zy.web.demo.duoxiancheng.demo26;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author <a href="mailto:zhouyong@cqyyt.com">zy</a>
 * @version 1.0
 * @date 2020/8/27 10:05
 * ReentrantLock下面的Condition实现等待/通知
 */
public class MyService26 {

    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void await(){
        lock.lock();
        try {
            //等待
            System.out.println("等待"+System.currentTimeMillis());
            condition.await();
        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void signal(){
        lock.lock();
        try {
            System.out.println("唤醒时间"+System.currentTimeMillis());
            //唤醒
            condition.signal();
        }finally {
            lock.unlock();
        }
    }

}
